
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Swaf.Gui.AspNet2
{
	public static class PageStateManager
	{
		public static string getPageStateSessionId(string viewName)
		{
			if (viewName != null && (viewName.EndsWith("popup.aspx") || viewName.StartsWith("popup.aspx") || viewName.EndsWith("popupchild.aspx")))
				return "PopupPageState";
			else
				return "ActivePageState";
		}

		public static bool isPopupState(string viewName)
		{
			return getPageStateSessionId(viewName) == "PopupPageState";
		}

		public static ViewOfflineState getState(string viewName, bool forceNew)
		{
			ViewOfflineState state = null;
			StateInfo pageState = App.sessionData[getPageStateSessionId(viewName)] as StateInfo;
			bool isPopup = isPopupState(viewName);
			if (pageState == null)
				App.sessionData[getPageStateSessionId(viewName)] = pageState = new StateInfo();
			if (isPopup)
			{
				if (forceNew)
					pageState.PageStates.Clear();
				if(pageState.PageStates.Count == 0)
					pageState.PageStates.Add(state = new ViewOfflineState(viewName));
				return pageState.PageStates[0];
			}

			if (!forceNew)
			{
				for (int p = pageState.Pos; p >= 0; p--)
				{
					ViewOfflineState s = pageState.PageStates[p];
					if (p == pageState.Pos && (viewName == null || viewName.EndsWith("child.aspx")))
						return s;
					if (s.PageId == viewName)
					{
						pageState.Pos = p;
						return s;
					}
				}
				for (int p = pageState.Pos + 1; p < pageState.PageStates.Count; p++)
				{
					ViewOfflineState s = pageState.PageStates[p];
					if (s.PageId == viewName)
					{
						pageState.Pos = p;
						return s;
					}
				}
			}
			else
			{
				if (pageState.PageStates.Count > 0 && pageState.PageStates[pageState.PageStates.Count - 1].PageId == viewName)
					pageState.PageStates[pageState.PageStates.Count - 1] = state = new ViewOfflineState(viewName);
				else
					pageState.PageStates.Add(state = new ViewOfflineState(viewName));
				pageState.Pos = pageState.PageStates.Count - 1;
			}
			if (pageState.PageStates.Count > s_maxPages)
			{
				pageState.PageStates.RemoveAt(0);
				pageState.Pos--;
			}
			Debug.Assert(state != null);
			return state;
		}
		public static IApplication App { get { return Swaf.Application.currentApp; } }
		private static int s_maxPages = 5;
	}

	[Serializable]
	public class StateInfo
	{
		public List<ViewOfflineState> PageStates = new List<ViewOfflineState>();
		public int Pos = -1;
	}
}
